package io.jee.medusa.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import io.jee.medusa.util.ResultModel;

public class DefaultFTPClientHelper implements FTPClientHelper {
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	private String hostname,username,password;
	private Integer port;
	
	public DefaultFTPClientHelper(String hostname, Integer port, String username,String password) {
		this.hostname = hostname;
		this.port = port;
		this.username = username;
		this.password = password;
	}
	
	@Override
	public ResultModel<Void> upload(String remotePath, String remoteName, File input) {
		try {
			return this.upload(remotePath, remoteName, new FileInputStream(input));
		} catch (FileNotFoundException e) {
			logger.error("FTP出错了", e);
			return ResultModel.result(false, -9, "反正就是上传出错了");
		}
	}
	
	@Override
	public ResultModel<Void> upload(String remotePath, String remoteName, InputStream input){
		FTPClient ftpClient = new FTPClient();
		try{
			ftpClient.setDefaultTimeout(60*1000);
			ftpClient.setDataTimeout(5*60*1000);
			
			if(port!=null){
				ftpClient.connect(hostname, port);
			}else{
				ftpClient.connect(hostname);
			}
			if(!ftpClient.login(username, password)){
				return ResultModel.result(false, -2, "FTP账号密码出错");
			}
			int reply = ftpClient.getReplyCode(); 
	        if (!FTPReply.isPositiveCompletion(reply)) { 
	        	ftpClient.disconnect(); 
	            return ResultModel.result(false, -1, "连接FTP服务器异常");
	        }
			if(!ftpClient.printWorkingDirectory().equals(remotePath)){
				ftpClient.changeWorkingDirectory(remotePath);
			}
			boolean success = ftpClient.storeFile(remoteName, input);
	        return ResultModel.result(success, "上传成功");
		}catch (Exception e){
			logger.error("FTP出错了", e);
			return ResultModel.result(false, -9, "反正就是上传出错了");
		}finally {
			try {
				input.close();
				if(!ftpClient.isConnected()){
					ftpClient.disconnect();
				}
			} catch (IOException e) {
			}
		}
        
	}
	
	
	public static void main(String[] args) throws SocketException, IOException {
		DefaultFTPClientHelper clientHelper = new DefaultFTPClientHelper("jpa.jee.io", null, "jpa2", "");
		for (int i = 0; i < 10; i++) {
			ResultModel<Void> result = clientHelper.upload("/tmp", "dsfd"+i, DefaultFTPClientHelper.class.getResourceAsStream("/1.htm"));
			System.out.println(result.getMessage());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100000000000000000l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
